package com.jackrain.nea.fc.ps.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 条码档案-接口
 * Mapper
 *
 * @author xuxinsuai
 * @create 2017/10/20
 */
@Mapper
public interface ViewCskuMapper {

    /**
     * 查询
     * @param param
     * @return List<HashMap>
     */
    @SelectProvider(type = ViewCskuSqlProvider.class,
            method = "selectskuarchsql")
    List<HashMap> selectskuarchsql(@Param("list") Object[] param);
    /**
     * 查询
     * @param param
     * @return List<HashMap>
     */
    @SelectProvider(type = ViewCskuSqlProvider.class,
            method = "selectskuclrsql")
    List<HashMap> selectskuclrsql(@Param("list") Object[] param);

    /**
     * 查询规格id
     * @param specobjid
     * @return long
     */
    @Select({"SELECT PS_C_SPEC_ID FROM PS_C_SPECOBJ WHERE ID=#{specobjid}"})
     long selectspecid(long specobjid);

    /**
     * 查询系统标示
     * @param specid
     * @return String
     */
    @Select({"SELECT FLAG FROM PS_C_SPEC WHERE ID=#{specid}"})
    String selectflagid(long specid);

    /**
     * 查询规格编码
     * @param specid
     * @return String
     */
    @Select({"SELECT ECODE FROM PS_C_SPECOBJ WHERE ID=#{specid}"})
    String selectspececood(long specid);

    /**
     * 查询
     * @param cproid
     * @return String
     */
    @Select({"SELECT ECODE FROM PS_C_PRODIM_ITEM WHERE ID=#{cproid}"})
    String selectprodimecode(long cproid);
    /**
     * 查询
     * @param cproid
     * @return String
     */
    @Select({"SELECT ECODE FROM PS_C_PRO WHERE ID=#{cproid}"})
    String selectproecode(long cproid);
    /**
     * 查询号型Group
     * @param cproid
     * @return String
     */
    @Select({"SELECT PS_C_SHAPEGROUP_ID FROM PS_C_PRO WHERE ID=#{cproid}"})
    Long selectshapegroup(long cproid);

    /**
     * 查询号型ECODE
     * @param shapegroupid
     * @paramsizeid
     * @return String
     */
    @Select({"SELECT ENAME FROM PS_C_SHAPE WHERE PS_C_SHAPEGROUP_ID=#{arg0} AND PS_C_SIZE_ID =#{arg1}"})
    String selectshapeecode(long shapegroupid,long sizeid);


    /**
     * 外键关联配销中心表
     * @param storeid
     * @return String
     */
    @Select({"SELECT ECODE FROM CP_C_STOREORG WHERE ID=#{storeid}"})
    String selectstoreecode(long storeid);
    /**
     * 跨库查询创建人/修改人
     * @param ownerid
     * @return String
     */
    @Select({"SELECT ENAME FROM USERS WHERE ID=#{ownerid}"})
    String selectusername(long ownerid);


    class ViewCskuSqlProvider {

        public String selectskuarchsql(final Map<String, Object> map) {
            return new SQL() {
                {

                    String colum = " ECODE,CP_C_DISTRIB_ID,PS_C_SPEC1OBJ_ID,PS_C_SPEC2OBJ_ID,PS_C_PRO_ID,STOPPUR,STOPREPLEN,PRONATURE,PURCHASEMODE,DISPALYSTANDARD,ISACTIVE,MODIFIERID,MODIFIEDDATE ";
                    SELECT( colum );
                    FROM("PS_C_SKU");
                    Object[] list = (Object[]) map.get("list");
                    StringBuilder sb = new StringBuilder("(");
                    for (Object num : list) {
                        sb.append(num).append(",");
                    }
                    sb.deleteCharAt(sb.length()-1).append(")");
                    WHERE("ID IN "+sb.toString());

                }
            }.toString();
        }


        public String selectskuclrsql(final Map<String, Object> map) {
            return new SQL() {
                {

                    String colum = " PS_C_PRO_ID,CREATIONDATE,OWNERID,CP_C_DISTRIB_ID,FABCOLOR,MAINCOLOR,PS_C_SPEC1OBJ_ID,PS_C_SPEC2OBJ_ID,MODIFIERID,MODIFIEDDATE  ";
                    SELECT( colum );
                    FROM("PS_C_SKU");
                    Object[] list = (Object[]) map.get("list");
                    StringBuilder sb = new StringBuilder("(");
                    for (Object num : list) {
                        sb.append(num).append(",");
                    }
                    sb.deleteCharAt(sb.length()-1).append(")");
                    WHERE("ID IN "+sb.toString());

                    GROUP_BY(" CP_C_DISTRIB_ID,PS_C_PRO_ID,PS_C_SPEC1OBJ_ID ");

                }
            }.toString();
        }

    }


}
